<?php

namespace addons\shopro\controller;

use addons\shopro\exception\Exception;
use addons\shopro\model\User;
use think\Db;

class Goods extends Base
{

    protected $noNeedLogin = ['index', 'detail', 'lists', 'activity', 'seckillList', 'grouponList', 'store','cityName'];
    protected $noNeedRight = ['*'];

    public function index()
    {
    }

    public function detail()
    {
        $order = Db::name("shopro_goods")->where(['deletetime' => null,'status'=>'up'])->find();
        $id = $this->request->get('id',24);
        if($order){
            $id =  $order['id'];
        }
        $is_dizhi = $this->request->get('is_dizhi');
        $detail = \addons\shopro\model\Goods::getGoodsDetail($id);
        
        // 记录足记
        \addons\shopro\model\UserView::addView($detail);

        $sku_price = $detail['sku_price'];      // 处理过的规格
        // tp bug json_encode 或者 toArray 的时候 sku_price 会重新查询数据库，导致被处理过的规格又还原回去了
        $detail = json_decode(json_encode($detail), true);
        $detail['sku_price'] = $sku_price;
        $one_province = config('site.one_province');
        $user = User::info();
        if($is_dizhi){
            $dizhi = explode('_',$is_dizhi);
            $arr = self::cityName($dizhi[1],$dizhi[0]);
            if($dizhi[0] && $dizhi[1] && $arr && strstr($one_province, $arr['result']['address_component']['city'])){
                $detail['original_price'] = 1000;
                $detail['price'] = 800;
                $detail['is_province'] = 1;
                $detail['is_sheng'] = 1;
                $detail['sku_price'][0]['price'] = 800;
                $detail['sku_price'][0]['stock'] = 1000;
            }else{
                $detail['original_price'] = 800;
                $detail['price'] = 600;
                $detail['is_province'] = 0;
                $detail['sku_price'][0]['price'] = 600;
                $detail['sku_price'][0]['stock'] = 800;
            }
        }else{
            $detail['original_price'] = 800;
            $detail['price'] = 600;
            $detail['is_province'] = 0;
            $detail['sku_price'][0]['price'] = 600;
            $detail['sku_price'][0]['stock'] = 800;
        }

        if($user){
           $order = Db::name("shopro_order")->where(['user_id'=>$user->id])->find();
           if($order){
               $detail['original_price'] = $detail['original_price']*0.98;
               $detail['price'] =  $detail['price']*0.98;
               $detail['sku_price'][0]['price'] = $detail['sku_price'][0]['price']*0.98;
               $detail['sku_price'][0]['stock'] = $detail['sku_price'][0]['stock']*0.98;
           }
        }

        $this->success('商品详情', $detail);
    }
    /**
     * 根据经纬度获取城市名称
     * @param $longitude
     * @param $latitude
     * @return mixed|string
     */
    public static function cityName($longitude=114.06933,$latitude=22.55251)
    {
        $key='3G5BZ-I2L36-DWMSV-EPA63-UIVB6-T7F2X';
        //调取腾讯接口,其中ak为key,注意location纬度在前，经度在后
        $api = "https://apis.map.qq.com/ws/geocoder/v1/?location=" . $latitude . "," . $longitude . "&output=json&pois=1&key=$key";
        $content = file_get_contents($api);
       // echo $content;
        $arr = json_decode($content, true);
        //echo $arr['result']['address_component']['province'];
        //print_r($arr);exit;
        if ($arr['status'] == 0) {
            return $arr;
        } else {
           return 'error';
        }
    }
    public function lists()
    {
        $params = $this->request->get();
        $data = \addons\shopro\model\Goods::getGoodsList($params);

        $this->success('商品列表', $data);

    }


    /**
     * 获取商品支持的 自提点
     */
    public function store()
    {
        $params = $this->request->get();
        $data = \addons\shopro\model\Goods::getGoodsStore($params);

        $this->success('自提列表', $data);
    }


    // 秒杀列表
    public function seckillList() {
        $params = $this->request->get();

        $this->success('秒杀商品列表', \addons\shopro\model\Goods::getSeckillGoodsList($params));
    }


    // 拼团列表
    public function grouponList() {
        $params = $this->request->get();

        $this->success('拼团商品列表', \addons\shopro\model\Goods::getGrouponGoodsList($params));
    }


    public function activity()
    {
        $activity_id = $this->request->get('activity_id');
        $activity = \addons\shopro\model\Activity::get($activity_id);
        if (!$activity) {
            $this->error('活动不存在', null, -1);
        }
        
        $goods = \addons\shopro\model\Goods::getGoodsList(['goods_ids' => $activity->goods_ids]);
        $activity->goods = $goods;
        
        $this->success('活动列表', $activity);
    }

    public function favorite()
    {
        $params = $this->request->post();
        $result = \addons\shopro\model\UserFavorite::edit($params);
        $this->success($result ? '收藏成功' : '取消收藏', $result);
    }

    public function favoriteList()
    {
        $data = \addons\shopro\model\UserFavorite::getGoodsList();
        $this->success('商品收藏列表', $data);
    }


    public function viewDelete()
    {
        $params = $this->request->post();
        $result = \addons\shopro\model\UserView::del($params);
        $this->success('删除成功', $result);
    }


    public function viewList()
    {
        $data = \addons\shopro\model\UserView::getGoodsList();
        $this->success('商品浏览列表', $data);
    }



}
